package com.google.project.client.ProjectManager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.project.client.Service.SubConstructorServiceAsync;
import com.google.project.client.Service.SubConstructorService;
import com.google.project.shared.ProjectDTO;
import com.google.project.shared.SubConstructorDTO;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.Label;

public class PMCreateSubConstructor extends Composite {

	private static final Binder binder = GWT.create(Binder.class);
	@UiField TextBox txtSCName;
	@UiField TextBox txtConsutantName;
	@UiField TextBox txtPhone;
	@UiField Button createBtn;
	@UiField Button cancelBtn;
	@UiField TextArea txtAreaDescription;
	@UiField Label lblTitle;

	private SubConstructorDTO currentSubConstructor = new SubConstructorDTO();
	private String projectId="";
	private ProjectDTO curProject = new  ProjectDTO();
	private final SubConstructorServiceAsync subConstructorService = GWT
			.create(SubConstructorService.class);

	interface Binder extends UiBinder<Widget, PMCreateSubConstructor> {
	}

	public PMCreateSubConstructor() {
		initWidget(binder.createAndBindUi(this));
	}

	/***
	 * contructor update subconstructor info
	 */
	public PMCreateSubConstructor(String subConstructorId,ProjectDTO project) {
		this();
		curProject =project; 
		getSubConstructorInfo(subConstructorId);
	}

	public PMCreateSubConstructor(ProjectDTO project, String action) {
		this.projectId = project.getProjectID();
		curProject = project;
		initWidget(binder.createAndBindUi(this));
		
	}

	private void getSubConstructorInfo(String subConstructorId) {
		// TODO Auto-generated method stub
		subConstructorService.getSubConstructorById(subConstructorId,
				new AsyncCallback<SubConstructorDTO>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Edit fail!");

					}

					@Override
					public void onSuccess(SubConstructorDTO result) {
						currentSubConstructor = result;
						displaySubConstructor();
					}

				});
	}

	private void displaySubConstructor() {
		// TODO Auto-generated method stub
		lblTitle.setText("CẬP NHẬT NHÀ THẦU PHỤ");
		txtSCName.setText(currentSubConstructor.getConstructorName());
		txtConsutantName.setText(currentSubConstructor.getConsultantName());
		txtPhone.setValue(currentSubConstructor.getPhone());
		txtAreaDescription.setText(currentSubConstructor.getDescription());
	}

	@UiHandler("cancelBtn")
	void onCancelBtnClick(ClickEvent event) {
		PMEntrypoint.get().PMSubConstructorList(curProject);
	}

	@UiHandler("createBtn")
	void onCreateBtnClick(ClickEvent event) {
		if (getFormValues()) {
			subConstructorService.createSubConstructor(currentSubConstructor,
					projectId, new AsyncCallback<SubConstructorDTO>() {

						@Override
						public void onSuccess(SubConstructorDTO result) {
							Window.alert("Lưu thành công!");
							PMEntrypoint.get().PMSubConstructorList(curProject);
						}

						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Có lỗi xảy ra!");

						}
					});
		}
	}

	private boolean getFormValues() {
		boolean flag = true;
		if (txtSCName.getText() != null
				&& !txtSCName.getText().equalsIgnoreCase("")) {
			currentSubConstructor.setConstructorName(txtSCName.getText());
			if (txtConsutantName.getText() != null
					&& !txtConsutantName.getText().equalsIgnoreCase("")) {
				currentSubConstructor.setConsultantName(txtConsutantName
						.getText());
				currentSubConstructor.setPhone(txtPhone.getText());
				currentSubConstructor.setDescription(txtAreaDescription
						.getText());
			} else {
				Window.alert("Bạn chưa nhập tên người đại diện!");
				return flag = false;
			}
		} else {
			Window.alert("Bạn chưa nhập tên nhà thầu phụ!");
			return flag = false;
		}

		return flag;
	}
}
